package com.http;

import android.util.Log;

import com.bean.AreaCode;
import com.bean.BankAccountAndPayChannel;
import com.bean.BankBranchData;
import com.bean.BankCodeData;
import com.bean.Banner;
import com.bean.BaseResponse;
import com.bean.CountyAreaCode;
import com.bean.HuiKaAreaCodeResponse;
import com.bean.HuiKaBankBranchCodeData;
import com.bean.HuiKaBankCodeData;
import com.bean.IndustryCategories;
import com.bean.MsAreaCode;
import com.bean.MsContactType;
import com.bean.UserInfo;
import com.bean.UserWallet;
import com.bean.VipLevel;
import com.google.gson.Gson;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.woke.data.BankCard;
import com.woke.data.BusinessCategory;
import com.woke.data.Category;
import com.woke.data.ExchangeType;
import com.woke.data.UnionPayChannel;
import com.woke.data.UserPromoteCode;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Field;

/**
 * 作者 ： 林凤河
 * 创建时间：2016/9/21
 * 备注：封装Retrofit2   RxJava2
 */

public class HttpMethods {
//    //线上域名
//    private static final String BASE_URL = "http://app.88china.com:8384/";
//    public static final String BASEURL = BASE_URL+"index.php?";//兼容旧的请求 线上域名
////    测试服务器域名
    private static final String BASE_URL = "http://120.78.79.235/";
    public static final String BASEURL = "http://120.78.79.235/index.php?";//兼容旧的请求 测试服务器域名

//    private static final String BASE_URL = "http://y191b18036.iok.la/";//联调url
//    public static final String BASEURL = "http://y191b18036.iok.la/index.php?";//联调用的url 郑
////
//    private static final String BASE_URL = "http://88china.6655.la/";//联调url
//    public static final String BASEURL = "http://88china.6655.la/index.php?";//联调用的url  三金


    // ?
    public static final String ZCT_URL = "http://app.88china.com:8384/index.php?s=/Api/Zcto/pullAndPush";//兼容旧的请求
    public static final String test = "http://y191b18036.iok.la/";//兼容旧的请求


    private Retrofit mRetrofit;
    private static final HttpMethods INSTANCE = new HttpMethods();
    private ApiInterface mApiInterface;

    public ApiInterface getApiInterface() {
        return mApiInterface;
    }

    private HttpMethods() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
//		builder.addInterceptor (new LogInterceptor ());
        builder.connectTimeout(60, TimeUnit.SECONDS);
        builder.readTimeout(100, TimeUnit.SECONDS);
        builder.addInterceptor(new CookiesAddInterceptor());
        builder.addInterceptor(new CookiesReceivedInterceptor());
        //缓存策略  已cookie 设置冲突
//		builder.addNetworkInterceptor (new ReWriteCacheControlInterceptor (App.newInstance ()));
//		File httpCacheDirectory = new File (App.newInstance ().getCacheDir (), "youthSaid");
//		Cache cache = new Cache (httpCacheDirectory, 1024 * 1024 * 10);
//		if (cache != null) {
//			builder.cache (cache);
//		}
        mRetrofit = new Retrofit.Builder()
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create(new Gson()))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(BASE_URL)
                .build();
        mApiInterface = createService(ApiInterface.class);
    }
    private HttpMethods(String url) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
//		builder.addInterceptor (new LogInterceptor ());
        builder.connectTimeout(4, TimeUnit.SECONDS);
        builder.readTimeout(4, TimeUnit.SECONDS);
        builder.addInterceptor(new CookiesAddInterceptor());
        builder.addInterceptor(new CookiesReceivedInterceptor());
        //缓存策略  已cookie 设置冲突
//		builder.addNetworkInterceptor (new ReWriteCacheControlInterceptor (App.newInstance ()));
//		File httpCacheDirectory = new File (App.newInstance ().getCacheDir (), "youthSaid");
//		Cache cache = new Cache (httpCacheDirectory, 1024 * 1024 * 10);
//		if (cache != null) {
//			builder.cache (cache);
//		}
        mRetrofit = new Retrofit.Builder()
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create(new Gson()))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(test)
                .build();
        mApiInterface = createService(ApiInterface.class);
    }

    public static HttpMethods getInstance() {
        return INSTANCE;
    }

    public <T> T createService(Class<T> service) {
        return mRetrofit.create(service);
    }


    public class HttpResultFunc<T> implements Function<BaseResponse<T>, T> {

        @Override
        public T apply(BaseResponse<T> tBaseResponse) throws Exception {
            if (tBaseResponse != null) {
                if (tBaseResponse.resultSuccess()) {
                    return tBaseResponse.getData().getData();
                } else {
                    throw new Exception(tBaseResponse.getInfo());
                }
            }
            throw new Exception("response is null");
        }
    }
    /**
     * -----------------------------------------------------------------------------------------
     */

    public Call<String> upIdcad(String user_id , File files) {
        List<MultipartBody.Part> params = new ArrayList<>();
        params.add(MultipartBody.Part.createFormData("user_id", user_id));
        Map<String, RequestBody> filsBodyMap = new HashMap<>();
        for (int i = 0; i < 1; i++) {
            File file = files;
            //			RequestBody requestBody = RequestBody.create (MediaType.parse ("image/jpg"), file);
            //			MultipartBody.Part part = MultipartBody.Part.createFormData ("file" + (i + 1), file.getName (), requestBody);
            //			filesPart.add (part);
            filsBodyMap.put("file" + (i + 1) + "\";filename=\"" + file.getName(), RequestBody.create(MediaType
                    .parse("image/*"), file));
        }
        return mApiInterface.upIdCard(params, filsBodyMap);
    }







    /**
     * -----------------------------------------------------------------------------------------
     */


    /**
     * 1.2 登陆
     *
     * @param observer
     * @param phone      手机号码
     * @param password   密码
     * @param registerid 极光推送注册ID JPushInterface.getRegistrationID(this);
     */
    public void login(Observer<UserInfo> observer, String phone, String password, String registerid) {
        Map<String, String> params = new HashMap<>();
        params.put("username", phone);
        params.put("password", password);
        params.put("registerid", registerid);

        mApiInterface.login(params).map(new HttpResultFunc<UserInfo>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * @param observer
     * @param user_id  用户id
     * @param nicename 姓名
     * @param userno   身份证号
     */
    public void isAuth(Observer<BaseResponse> observer, String user_id, String nicename, String userno, List<File> files) {

        List<MultipartBody.Part> params = new ArrayList<>();
        params.add(MultipartBody.Part.createFormData("user_id", user_id));
        params.add(MultipartBody.Part.createFormData("nicename", nicename));
        params.add(MultipartBody.Part.createFormData("userno", userno));
        Map<String, RequestBody> filsBodyMap = new HashMap<>();

        for (int i = 0; i < files.size(); i++) {
            File file = files.get(i);
//			RequestBody requestBody = RequestBody.create (MediaType.parse ("image/jpg"), file);
//			MultipartBody.Part part = MultipartBody.Part.createFormData ("file" + (i + 1), file.getName (), requestBody);
//			filesPart.add (part);
            filsBodyMap.put("file" + (i + 1) + "\";filename=\"" + file.getName(), RequestBody.create(MediaType
                    .parse("image/*"), file));
        }
        mApiInterface.idAuth(params, filsBodyMap)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 我的钱包
     *
     * @param observer
     * @param userLogin 登录账号
     */
    public void userWallet(Observer<UserWallet> observer, String userLogin) {
        mApiInterface.userWallet(userLogin)
                .map(new HttpResultFunc<UserWallet>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    /**
     * 5.00 广告栏目
     *
     * @param
     */
    public void getSystemKg(int type,int oem_id,Observer<String> observer) {
//        mApiInterface.kaiGuan(type, oem_id)
//                .map(new HttpResultFunc<String>())
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(observer);

    }


    /**
     * 1.41 广告栏目
     *
     * @param observer
     */
    public void getBannerList(Observer<List<Banner>> observer,int oem_id) {
        mApiInterface.getBannerList(oem_id)
                .map(new HttpResultFunc<List<Banner>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 1.42 我的银行卡
     *
     * @param observer
     * @param userid
     */
    public void getBankCardList(Observer<List<BankCard>> observer, int userid) {
        mApiInterface.getBankCardList(userid)
                .map(new HttpResultFunc<List<BankCard>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    public void getBankCardList(Observer<List<BankCard>> observer, int userid, int kind) {
        mApiInterface.getBankCardList(userid, kind)
                .map(new HttpResultFunc<List<BankCard>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.1中交所
     *
     * @param observer
     * @param type
     */
    public void exchangeOrgList(Observer<List<UserPromoteCode>> observer, int type) {
        mApiInterface.exchangeOrgList(type)
                .map(new HttpResultFunc<List<UserPromoteCode>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 信用卡提现
     *
     * @param observer
     */
    public void cashAdvance(Observer<BaseResponse> observer, int user_id, float amount, String name, String card_no, int way) {

        mApiInterface.cashAdvance(user_id, amount, name, card_no, way)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }


    /**
     * 判断银行卡是否绑定快捷支付
     *
     * @param observer
     * @param user_id
     * @param way
     * @param card_no
     */
    public void isBindedBankAccount(Observer<BaseResponse> observer, int user_id, int way, String card_no) {
        mApiInterface.isBindedBankAccount(user_id, way, card_no)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 绑卡
     *
     * @param observer
     */
    public void bindingBankAccount(Observer<BaseResponse<String>> observer, int user_id, int way, String card_no) {
        mApiInterface.bindingBankAccount(user_id, way, card_no)
//				.map (new HttpResultFunc<String> ())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 先判定是否绑卡,有绑卡直接 调用充值支付
     *
     * @param observer
     * @param user_id
     * @param amount
     * @param way
     * @param card_no
     */
    public void isBindedToRecharge(Observer<BaseResponse> observer, final int user_id, final float amount, final int way, final String card_no) {
        mApiInterface.isBindedBankAccount(user_id, way, card_no)
                .flatMap(new Function<BaseResponse, ObservableSource<BaseResponse>>() {
                    @Override
                    public ObservableSource<BaseResponse> apply(BaseResponse baseResponse) throws Exception {
                        if (baseResponse != null) {
                            if (baseResponse.resultSuccess()) {
                                //已绑卡
                                return mApiInterface.recharge(user_id, amount, way, -1, card_no);
                            }
                            Log.e("isBinded", baseResponse.getInfo());
                            throw new Exception("unbind");//未绑卡,则抛出异常,让Observer<BaseResponse> 在onError接受
                        }
                        throw new Exception("response is null");
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 获取银联的支付通道和用户的卡信息
     *
     * @param observer
     * @param userid
     * @param type     1--支付、2--收款
     * @param kind     1--微信、2--支付宝、3--银联
     */
    public void getBankAccountAndUnionPayChannel(Observer<BankAccountAndPayChannel> observer, int userid, int type, int kind) {
        mApiInterface.getBankAccountAndUnionPayChannel(userid, type, kind)
                .map(new HttpResultFunc<BankAccountAndPayChannel>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);

    }

    /**
     * 获取支付渠道
     *
     * @param observer
     * @param userid
     * @param type     1--支付、2--收款
     * @param kind     1--微信、2--支付宝、3--银联
     */
    public void getPayChanel(Observer<List<UnionPayChannel>> observer, int userid, int type, int kind) {
        mApiInterface.getPayChannel(userid, type, kind)
                .map(new HttpResultFunc<List<UnionPayChannel>>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 激活商家收款码
     *
     * @param observer     返回的数据为  商家激活码（与serialNumber一样）
     * @param userId       用户id
     * @param serialNumber 商家收款码系列号
     */
    public void activateSeriesNo(Observer<String> observer, int userId, String serialNumber) {
        mApiInterface.activateSeriesNo(userId, serialNumber)
                .map(new HttpResultFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);

    }

    /**
     * 3.8 商户入驻信息
     *
     * @param observer 返回string 0都未入驻 1微信入驻，支付宝未入驻 2支付宝入驻，微信为入驻 3都已入驻
     * @param userId
     */
    public void getMerchantApi(Observer<Integer> observer, int userId) {
        mApiInterface.getMerchantApi(userId)
                .map(new HttpResultFunc<Integer>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.9 获取商户支付类型及对应的经营栏目
     *
     * @param observer
     * @param userId
     */
    public void getPayway(Observer<List<BusinessCategory>> observer, int userId) {
        mApiInterface.getPayway(userId)
                .map(new HttpResultFunc<List<BusinessCategory>>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.91 获取微信/支付宝  商户经营栏目
     *
     * @param observer
     * @param payWay   1微信 2支付宝  经营栏目
     */
    public void getBusinessCategory(Observer<List<Category>> observer, int payWay) {
        mApiInterface.getBusinessCategory(payWay)
                .map(new HttpResultFunc<List<Category>>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.10 提交商户入驻信息
     *
     * @param observer
     * @param userId          用户ID
     * @param payWay          支付类型1:微信 2支付宝
     * @param merchantName    商户名称
     * @param shortName       商户简称
     * @param merchantAddress 商户地址
     * @param servicePhone    客服电话
     * @param contactName     联系人姓名
     * @param contactMobile   联系人手机号
     * @param category        经营栏目
     * @param idCard          商户身份证号
     * @param accNo           收款人银行卡号
     * @param accName         收款人姓名
     * @param bankName        开户行
     * @param remark          备注说明
     */
    public void merchantInfo(Observer<BaseResponse> observer, int userId, int payWay, String merchantName, String shortName, String merchantAddress, String servicePhone, String contactName, String contactMobile, String category, String idCard, String accNo, String accName, String bankName, String remark) {
        Map<String, String> map = new HashMap<>();
        map.put("userId", String.valueOf(userId));
        map.put("payWay", String.valueOf(payWay));
        map.put("merchantName", merchantName);
        map.put("shortName", shortName);
        map.put("merchantAddress", merchantAddress);
        map.put("servicePhone", servicePhone);
        map.put("contactName", contactName);
        map.put("contactMobile", contactMobile);
        map.put("category", category);
        map.put("idCard", idCard);
        map.put("accNo", accNo);
        map.put("accName", accName);
        map.put("bankName", bankName);
        map.put("remark", remark);
        mApiInterface.merchantInfo(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }


    /**
     * 3.11
     *
     * @param observer
     * @param userId
     * @param serialNumber
     * @param gallery      通道字符串逗号，隔开  类似1,2,3
     */
    public void openChannelAndActivateSerierNo(Observer<BaseResponse> observer, int userId, String serialNumber, String gallery) {

        mApiInterface.openChannelAndActivateSerierNo(userId, serialNumber, gallery)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.15获取已开通的全部类型的收款通道接口(收款码专用)
     *
     * @param observer
     * @param userId
     * @param serialNumber 商家系列号
     */
    public void getMerchatCollectionChannel(Observer<List<UnionPayChannel>> observer, int userId, String serialNumber) {
        mApiInterface.getMerchatCollectionChannel(userId, serialNumber)
                .map(new HttpResultFunc<List<UnionPayChannel>>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.17 新增通道
     *
     * @param observer 返回 系类好
     * @param userId
     */
    public void newEnterGall(Observer<String> observer, int userId) {
        mApiInterface.newEnterGall(userId)
                .map(new HttpResultFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.18 汇卡 省列表
     *
     * @param observer
     */
    public void getHuiKaProvinceAreaCode(Observer<HuiKaAreaCodeResponse> observer) {
        mApiInterface.getHuiKaProvinceAreaCode()
                .map(new HttpResultFunc<HuiKaAreaCodeResponse>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.19 汇卡 市级列表（分页）
     *
     * @param observer
     * @param p_code
     * @param page
     */
    public void getHuiKaCityAreaCode(Observer<HuiKaAreaCodeResponse> observer, String p_code, int page) {
        mApiInterface.getHuiKaCityAreaCode(p_code, page)
                .map(new HttpResultFunc<HuiKaAreaCodeResponse>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.20 广东汇卡--获取银行总行ID
     *
     * @param observer
     * @param page     页码
     * @param bankName 模糊查询  银行（总行）
     */
    public void getHuiKaBankList(Observer<HuiKaBankCodeData> observer, int page, String bankName) {
        mApiInterface.getHuiKaBankList(page, bankName)
                .map(new HttpResultFunc<HuiKaBankCodeData>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.21 广东汇卡--获取银行支行行号
     *
     * @param observer
     * @param bankPid  3.20 接口 总行id
     * @param page     页码
     * @param bankName 模糊查询  银行（支行）
     */
    public void getHuiKaBranchBankList(Observer<HuiKaBankBranchCodeData> observer, int bankPid, int page, String bankName) {
        mApiInterface.getHuiKaBranchBankList(bankPid, page, bankName)
                .map(new HttpResultFunc<HuiKaBankBranchCodeData>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.40 广东汇卡  商户入驻
     *
     * @param observer
     * @param map
     */
    public void huiKaiMerchantsEnter(Observer<BaseResponse> observer, Map<String, String> map) {
        mApiInterface.huiKaiMerchantsEnter(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.43 广州汇卡 判断是否需要重新提交商户入驻信息
     *
     * @param observer
     * @param userId
     * @param way
     */
    public void isSettled(Observer<BaseResponse> observer, @Field("userId") int userId, @Field("way") int way) {
        mApiInterface.isSettled(userId, way)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 商家收款码
     *
     * @param observer
     * @param userId
     */
    public void businessSeriesNo(Observer<String> observer, int userId) {
        mApiInterface.businessSeriesNo(userId)
                .map(new HttpResultFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);

    }

    /**
     * 旧的商家收款码
     *
     * @param observer
     * @param userid
     */
    public void oldBusinessSeriesNo(Observer<String> observer, int userid) {
        mApiInterface.oldBusinessSeriesNo(userid)
                .map(new HttpResultFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);

    }

    /**
     * 3.263.26众安车险
     *
     * @param observer
     * @param userid
     */
    public void fetchDirectUrl(Observer<String> observer, int userid) {
        mApiInterface.fetchDirectUrl(userid)
                .map(new HttpResultFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);

    }

    /**
     * 3.27获取中交所类型列表
     *
     * @param observer
     */
    public void getExchangeType(Observer<List<ExchangeType>> observer) {
        mApiInterface.getExchangeType()
                .map(new HttpResultFunc<List<ExchangeType>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.29 浦发 行业类别
     *
     * @param observer
     */
    public void getIndustryCategories(Observer<List<IndustryCategories>> observer) {
        mApiInterface.getIndustryCategories()
                .map(new HttpResultFunc<List<IndustryCategories>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.30 浦发 省列表
     *
     * @param observer
     */
    public void getProvinceAreaCode(Observer<List<AreaCode>> observer) {
        mApiInterface.getProvinceAreaCode()
                .map(new HttpResultFunc<List<AreaCode>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.31浦发 市列表
     *
     * @param observer
     * @param areaCode
     */
    public void getCityAreaCode(Observer<List<AreaCode>> observer, String areaCode) {
        mApiInterface.getCityAreaCode(areaCode)
                .map(new HttpResultFunc<List<AreaCode>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.46 浦发 县列表
     *
     * @param observer
     * @param city_no
     */
    public void getCountyCode(Observer<List<CountyAreaCode>> observer, String city_no) {
        mApiInterface.getCountyCode(city_no)
                .map(new HttpResultFunc<List<CountyAreaCode>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.32 浦发开户总行列表
     *
     * @param observer
     * @param page
     * @param bankName
     */
    public void getBankList(Observer<BankCodeData> observer, int page, String bankName) {
        mApiInterface.getBankList(page, bankName)
                .map(new HttpResultFunc<BankCodeData>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.33 浦发开户支行列表
     *
     * @param observer
     * @param bankId   总行编码id  (数字字符串)
     * @param page
     * @param bankName
     */
    public void getBranchAndLinenum(Observer<BankBranchData> observer, String bankId, int page, String bankName) {
        mApiInterface.getBranchAndLinenum(bankId, page, bankName)
                .map(new HttpResultFunc<BankBranchData>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.34 浦发商户入驻
     *
     * @param observer
     * @param map
     */
    public void puFaMerchantsEnter(Observer<BaseResponse> observer, Map<String, String> map) {
        mApiInterface.puFaMerchantsEnter(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.23入驻列表
     *
     * @param observer
     * @param userId
     */
    public void gallery_list(Observer<List<UnionPayChannel>> observer, int userId) {
        mApiInterface.gallery_list(userId)
                .map(new HttpResultFunc<List<UnionPayChannel>>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.24 通道入驻
     *
     * @param observer
     * @param userId
     * @param galleryWay
     */
    public void merchantSettledApi(Observer<BaseResponse> observer, int userId, int galleryWay) {
        mApiInterface.merchantSettledApi(userId, galleryWay)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.35 判断身份证正反面图片是否审核通过(浦发银行)
     *
     * @param observer
     * @param userId
     */
    public void idCardPhotoAuthState(Observer<BaseResponse> observer, int userId) {
        mApiInterface.idCardPhotoAuthState(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.36 	广州浦发入驻上传图片
     *
     * @param observer
     * @param userid
     * @param frontFile
     * @param reverseFile
     */
    public void picSec(Observer<BaseResponse> observer, int userid, File frontFile, File reverseFile) {
        List<MultipartBody.Part> params = new ArrayList<>();
        params.add(MultipartBody.Part.createFormData("userId", String.valueOf(userid)));


        Map<String, RequestBody> filsBodyMap = new HashMap<>();
        filsBodyMap.put("photo1\";filename=\"" + frontFile.getName(), RequestBody.create(MediaType
                .parse("image/*"), frontFile));
        filsBodyMap.put("photo2\";filename=\"" + reverseFile.getName(), RequestBody.create(MediaType
                .parse("image/*"), reverseFile));

        mApiInterface.picSec(params, filsBodyMap)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.37 	广州浦发--判断商户信息是否已入驻
     *
     * @param observer
     * @param userId
     */
    public void ifEnter(Observer<BaseResponse> observer, int userId) {
        mApiInterface.ifEnter(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.38 广州浦发--商户选择开通的支付类型
     *
     * @param observer
     * @param way
     */
    public void openChoosePayType(Observer<BaseResponse> observer, int userId, int way) {
        mApiInterface.openChoosePayType(userId, way)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 浦发开通支付通道流程
     *
     * @param observer
     * @param userId
     * @param way
     */
    public void pufaAuthState(Observer<BaseResponse> observer, final int userId, final int way) {
        mApiInterface.ifEnoughTime(userId, way)
                .flatMap(new Function<BaseResponse, ObservableSource<BaseResponse>>() {
                    @Override
                    public ObservableSource<BaseResponse> apply(BaseResponse baseResponse) throws Exception {
                        if (baseResponse != null) {
                            if (baseResponse.resultSuccess()) {
                                //3.39 广州浦发--商户入驻信息提交后,至少需要20s,才后开通支付通道3.38(浦发规定)
                                //商户信息审核通过-->开通通道  (审核通过,则表示身份证和入驻信息都通过了)
                                //即mApiInterface.idCardPhotoAuthState(userId) 和 mApiInterface.ifEnter(userId) 返回的都是success
                                return mApiInterface.idCardPhotoAuthState(userId);
                            } else if ("60001".equals(baseResponse.getFlag())) {
                                //商户信息正在审核中...
                                throw new Exception("in_the_review");
                            } else {
                                //返回unKnow 表示商户信息没提交过,-->调用是否要上传身份证
                                return mApiInterface.idCardPhotoAuthState(userId);
                            }
                        }
                        throw new Exception("response is null");
                    }
                })
                .flatMap(new Function<BaseResponse, ObservableSource<BaseResponse>>() {
                    @Override
                    public ObservableSource<BaseResponse> apply(BaseResponse baseResponse) throws Exception {
                        if (baseResponse != null) {
                            if (baseResponse.resultSuccess()) {
                                //浦发身份证照审核通过-->是否已经填写入驻信息
                                return mApiInterface.ifEnter(userId);
                            }
                            throw new Exception("idcardUnAuth");//
                        }
                        throw new Exception("response is null");
                    }
                })
                .flatMap(new Function<BaseResponse, ObservableSource<BaseResponse>>() {
                    @Override
                    public ObservableSource<BaseResponse> apply(BaseResponse baseResponse) throws Exception {
                        if (baseResponse != null) {
                            if (baseResponse.resultSuccess()) {
                                //浦发入驻信息审核通过-->开通支付通道
                                return mApiInterface.openChoosePayType(userId, way);
                            }
                            throw new Exception("enterInfoUnAuth");//
                        }
                        throw new Exception("response is null");
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.41 用户可升等级列表
     *
     * @param observer
     * @param userId
     */
    public void getVipLevel(Observer<List<VipLevel>> observer, int userId) {
        mApiInterface.getVipLevel(userId)
                .map(new HttpResultFunc<List<VipLevel>>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.42 用户升级新接口
     *
     * @param observer
     * @param userId
     * @param viplevel
     */
    public void memberUpApi(Observer<BaseResponse> observer, int userId, int viplevel) {
        mApiInterface.memberUpApi(userId, viplevel)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.44 民生  商户入驻新接口
     *
     * @param observer
     * @param map
     */
    public void newSettled(Observer<BaseResponse> observer, Map<String, String> map) {
        mApiInterface.newSettled(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.47 民生  省市县
     *
     * @param observer
     * @param parentId
     */
    public void getAreaCode(Observer<List<MsAreaCode>> observer, String parentId) {
        mApiInterface.getAreaCode(parentId)
                .map(new HttpResultFunc<List<MsAreaCode>>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 3.47 负责人类型
     *
     * @param observer
     */
    public void getContactType(Observer<List<MsContactType>> observer) {
        mApiInterface.getContactType()
                .map(new HttpResultFunc<List<MsContactType>>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

//	/**
//	 * 2.5视频专题(换一批)
//	 *
//	 * @param observer
//	 */
//	public void getVideoFeatureListByRandom(Observer<List<VideoFeature>> observer) {
//		mApiInterface.getVideoFeatureListByRandom ()
//				.map (new HttpResultFunc<List<VideoFeature>> ())
//				.subscribeOn (Schedulers.io ())
//				.unsubscribeOn (Schedulers.io ())
//				.observeOn (AndroidSchedulers.mainThread ())
//				.subscribe (observer);
//	}

//	/**
//	 * 2.8.1 首页下半部分具体视频 默认获取一个分类的视频
//	 *
//	 * @param observer
//	 * @param pid
//	 * @param limit
//	 */
//	public void getCouseListByPidAndLimit(Observer<CourseListCall> observer, int pid, final int limit) {
//		mApiInterface.getCourseClass (pid)
//				.map (new HttpResultFunc<List<CourseClass>> ())
//				.flatMap (new Function<List<CourseClass>, ObservableSource<CourseListCall>> () {
//					@Override
//					public ObservableSource<CourseListCall> apply(List<CourseClass> courseClasses) throws Exception {
//						if (courseClasses != null) {
//							int courseSort = courseClasses.get (0).getSortId ();
//							return mApiInterface.getCourseListByLimit (courseSort, limit);
//						}
//						return mApiInterface.getCourseListByLimit (20, limit);
//					}
//				})
//				.subscribeOn (Schedulers.io ())
//				.unsubscribeOn (Schedulers.io ())
//				.observeOn (AndroidSchedulers.mainThread ())
//				.subscribe (observer);
//	}

}
